package com.ld.security.demo.modules.admin.api;

import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.common.core.util.TemplateUtil;
import com.ld.security.common.tomcat.util.RespUtil;
import com.ld.security.server.admin.modules.ge.entity.GeCompEntity;
import com.ld.security.server.admin.modules.ge.service.IGeCompService;
import com.ld.security.server.admin.modules.gl.entity.GlConfigEntity;
import com.ld.security.server.admin.modules.gl.entity.GlEnumEntity;
import com.ld.security.server.admin.modules.gl.entity.GlEnumItemEntity;
import com.ld.security.server.admin.modules.gl.entity.GlFuncEntity;
import com.ld.security.server.admin.modules.gl.service.IGlConfigService;
import com.ld.security.server.admin.modules.gl.service.IGlEnumItemService;
import com.ld.security.server.admin.modules.gl.service.IGlEnumService;
import com.ld.security.server.admin.modules.gl.service.IGlFuncService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/config")
public class ConfigApi {

    @Autowired
    IGlEnumService iGlEnumService;
    @Autowired
    IGlEnumItemService iGlEnumItemService;

    @Autowired
    IGlFuncService iGlFuncService;

    @Autowired
    IGeCompService iGeCompService;

    @Autowired
    IGlConfigService iGlConfigService;


    private List getCompList(){
        List<GeCompEntity> comps = iGeCompService.all();
        List data = comps.stream().map(v -> {
            Map map = new HashMap<>();
            map.put("value", v.getId());
            map.put("label", v.getLabel());
            map.put("code", v.getCode());
            return map;
        }).collect(Collectors.toList());
        return data;
    }


    @ApiOperation("获得组件配置")
    @RequestMapping(value = "/getCompConfig", method = RequestMethod.GET)
    public void getCompConfig() {

        List data = this.getCompList();
        HashMap map = new HashMap<>();
        map.put("json", JsonUtil.toJson(data));
        String js = TemplateUtil.renderTemplate("/js/glCompConfig.js", map);
        RespUtil.writeJson(js);
    }


    @ApiOperation("获得字典配置")
    @RequestMapping(value = "/getEnumConfig", method = RequestMethod.GET)
    public void getEnumConfig() {
        List<GlEnumItemEntity> enumItems = iGlEnumItemService.all();
        List<GlEnumEntity> enumEntities = iGlEnumService.all();
        Map<String, List<GlEnumItemEntity>> enumItemMap = enumItems.stream().collect(Collectors.groupingBy(v -> v.getEnumId()));

        HashMap args = new HashMap<>();

        for (GlEnumEntity en : enumEntities) {
            List<GlEnumItemEntity> list = enumItemMap.get(en.getId());
            if(list == null){
                log.warn("异常枚举："+en.getCode());
                continue;
            }
            list.sort((x,y) -> {
                return x.getSortVal() - y.getSortVal();
            });
            List<Map> maps = list.stream()
                    .map(v -> {
                        Map enumItem = new HashMap<>();
                        enumItem.put("label", v.getLabel());
                        enumItem.put("value", v.getValue());
                        return enumItem;
                    }).collect(Collectors.toList());
            args.put(en.getCode(), maps);
        }
        //特殊组件枚举用
        args.put("ge_comp",this.getCompList());


        Map map = new HashMap<>();
        map.put("json", JsonUtil.toJson(args));
        String js = TemplateUtil.renderTemplate("/js/glEnumConfig.js", map);
        RespUtil.writeJson(js);
    }


    @ApiOperation("获得函数配置")
    @RequestMapping(value = "/getFuncConfig", method = RequestMethod.GET)
    public void getFuncConfig() {
        List<GlFuncEntity> funcEntities = iGlFuncService.all();
        List<Map> maps = funcEntities.stream().map(v -> {
            Map map = new HashMap<>();
            map.put("label", v.getLabel());
            map.put("value", v.getCode());
            return map;
        }).collect(Collectors.toList());

        HashMap map = new HashMap<>();
        map.put("funcs", funcEntities);
        map.put("json", JsonUtil.toJson(maps));
        String js = TemplateUtil.renderTemplate("/js/glFuncConfig.js", map);
        RespUtil.writeJson(js);
    }

    GlConfigEntity defConfig = new GlConfigEntity();


    private String javaCode="java";


    @ApiOperation("获得系统配置")
    @RequestMapping(value = "/getSysConfig", method = RequestMethod.GET)
    public void getSysConfig(String code) {

        GlConfigEntity e =  iGlConfigService.getDefultConfig(code);

        if(e == null){
          e=defConfig;
        }
        if(e != null){
            HashMap map = new HashMap<>();

            map.put("json", JsonUtil.toJson(e));
            String js = TemplateUtil.renderTemplate("/js/glSysConfig.js", map);
            RespUtil.writeJson(js);
        }

    }

    }
